package swd.maintain.system.backup_project_table;

import com.pub.database.*;
import com.pub.web.WebFunction;
import com.pub.tools.*;

import swd.base.SwdBase;

import java.util.*;

public class Business extends SwdBase{
    QueryBean gqbnDAO=null;
    String gstrDBAlias=null;
    public Business(QueryBean pqbnDAO,String pstrDBAlias){
        super(null,null,pqbnDAO);
        gqbnDAO=pqbnDAO;
        gstrDBAlias=pstrDBAlias;
   }
   /**
    * getData
    *
    * @param view View
    * @param pstrUserNO String
    * @return String
    */
   String getData(View view, String pstrUserNO) throws Exception {
       if(view.getDataKind().equals("tables")){
           return getTables(view.
                   getsys_no());
       }

       return "";
    }
   public String getSysNoList(String pstrDefaultSys){
       return WebFunction.transferRSToHtmlOption(gqbnDAO,"select sys_no,sys_no " + DBFunction.getConStrSybtax(DBFunction.getDBType(gstrDBAlias)) + " ' ' " + DBFunction.getConStrSybtax(DBFunction.getDBType(gstrDBAlias)) + " sys_name from wkf_sys order by sys_no",pstrDefaultSys);
   }

   public String getDbfNoList(String pstrSysNo) throws Exception{
       StringBuffer lstbXml=new StringBuffer();
       Hashtable<String,Hashtable<String,Object>> lhtFields=null;
       Hashtable<String,Object> lhtData=null;
       try{
           lhtFields=gqbnDAO.hashtableSQL("select dbf_no,dbf_no " + DBFunction.getConStrSybtax(DBFunction.getDBType(gstrDBAlias)) + " ' ' " + DBFunction.getConStrSybtax(DBFunction.getDBType(gstrDBAlias)) + " dbf_name from wkf_table where sys_no='" + pstrSysNo + "' order by dbf_no");

           if(lhtFields==null){
               return (new View()).getMessage("no_schema");
           }
           else{
               lstbXml.append("<TABLE>");
           }

           for(int i=0;i<lhtFields.size();i++){
               lhtData=((Hashtable<String,Object>)lhtFields.get(String.valueOf(i)));
               lstbXml.append("<TR>");
               lstbXml.append("<TD>");
               lstbXml.append("<input type='checkbox' name='trans_tables' value='");
               lstbXml.append(lhtData.get("DBF_NO"));
               lstbXml.append("'>");
               lstbXml.append("</TD>");
               lstbXml.append("<TD>");
               lstbXml.append(lhtData.get("DBF_NO"));
               lstbXml.append("</TD>");
               lstbXml.append("<TD>");
               lstbXml.append(lhtData.get("DBF_NAME"));
               lstbXml.append("</TD>");
               lstbXml.append("</TR>");
           }

           lstbXml.append("<TR><TD></TD><TD></TD><TD></TD></TR>");
           lstbXml.append("<TR><TD colspan='3'><input type='button' value='Select All' onclick=\"for(i=0;i<document.forms[0].trans_tables.length;i++){document.forms[0].trans_tables[i].checked=true;}\"/><input type='button' value='Unselect All' onclick=\"for(i=0;i<document.forms[0].trans_tables.length;i++){document.forms[0].trans_tables[i].checked=false;}\"/></TD></TR>");
           lstbXml.append("</table>");

           return lstbXml.toString().trim();
       }
       catch(Exception e){
           throw new Exception(e);
       }
       finally{
           if(lhtData!=null){
               lhtData.clear();
               lhtData = null;
           }
           if(lhtFields!=null){
               lhtFields.clear();
               lhtFields = null;
           }
       }

   }

   public String getTables(String pstrSysNo){
       ArrayList<ArrayList<Object>> laltTableList=null;
       String lstrResult=null;
       try{
           laltTableList=gqbnDAO.querySQL("select wkf_table.dbf_no " + DBFunction.getConStrSybtax(DBFunction.getDBType(gstrDBAlias)) + " '/' " + DBFunction.getConStrSybtax(DBFunction.getDBType(gstrDBAlias)) + " wkf_table.dbf_name from wkf_sys_table,wkf_table where wkf_sys_table.dbf_no=wkf_table.dbf_no and wkf_sys_table.sys_no='" + pstrSysNo + "'");
           lstrResult=laltTableList.toString();
           return PublicFunction.replace(PublicFunction.replace(lstrResult,"[","",false),"]","",false);
       }
       catch(Exception e){
           return "null";
       }
       finally{
           if(laltTableList!=null){
               laltTableList.clear();
               laltTableList = null;
           }
       }
   }
}
